<?php
namespace Admin\Controller;
use Think\Controller;
class UserController extends CommonController {
    public function index(){
    	
        $this->show();
    }
   // 会员列表
     public function member_list(){
              // 分页
      $count= M('user')->count();// 查询满足要求的总记录数
      $Page = new \Think\Page($count,3);// 实例化分页类 传入总记录数和每页显示的记录数(8)
      $show  = $Page->show();// 分页显示输出

      $this->assign('page',$show);// 赋值分页输出 
   
      $sql="select *from user where is_admin = '0' and del_status='0' limit ".$Page->firstRow.','.$Page->listRows ;
      $user=M()->query($sql);
      $this->assign('users',$user);
       
      $this->show();

     }
    // 删除会员
    public function member_del(){
        $sql='select * from user where del_status="1"' ;    
        $users=M()->query($sql);
        $this->assign('users',$users);
    	$this->show();
    }

    // 批量删除会员
    public  function member_delAll(){
         $str=I('post.res');
         $str=rtrim($str,",");
         if (M('user')->delete($str)) {
           $arr=array(
             'error'=>1,
             'info'=>'已删除！'
            );
           echo json_encode($arr);
           exit;
         }else{
            $arr=array(
             'error'=>2,
             'info'=>'删除失败！'
            );
           echo json_encode($arr);
           exit;
         }


    }
    // 会员积分
    public function member_kiss(){


    	$this->show();
    }
    // 浏览记录
    public function member_view(){
       


       $this->show();
    }
   // 会员等级
   public  function  member_level(){


    $this->show();
   }
    // 会员展示
    public function member_show(){
       $id=I('get.id');
       $member=M('user')->find($id);
       $this->assign('member',$member);
        $this->show();
    }
    // 会员增加
    public function member_add(){
        
          $this->show();
    }

    // 增加会员信息核对
    public function member_checkadd(){
        // var_dump(I('post.'));
       if (M('user')->where(array('email'=>I('post.email')))->find()) {
           $arr=array(
               'error'=>1,
               'info'=>'邮箱已被使用,请更换邮箱'
            );
           echo json_encode($arr);
           exit;
       }elseif (M('user')->where(array('nickname'=>I('post.username')))->find()) {
           $arr=array(
               'error'=>2,
               'info'=>'昵称已被使用，请更换昵称'
            );
           echo json_encode($arr);
           exit; 
       }else{
        $data['email']=I('post.email');
        $data['nickname']=I('post.username');
        $data['password']=md5(I('post.pass'));
        $data['create_time']=time();
        if (M('user')->add($data)) {
             $arr=array(
               'error'=>3,
               'info'=>'添加成功'
            );
           echo json_encode($arr);
           exit; 
        }
       }
    }
    // 会员编辑
    public function member_edit(){

        $this->show();
    }
    
    // 会员密码修改
    public function member_password(){
       $id=I('get.');

       $user=M('user')->where($id)->find();
       var_dump($user);
       $this->assign('user',$user);
        $this->show();
    }
    // 会员密码修改确认
    public function member_checkpassword(){
 
       // var_dump(I('post.'));
               $id=I('post.uid');
               // var_dump($id);
               $data['password']=md5(I('post.newpass'));
             if (M('user')->where(array('id'=>$id))->find()) {
                   if (M('user')->where(array('id'=>$id))->save($data)) {
                      $arr=array(
                        'error'=>1,
                        'info'=>'修改密码成功'
                        );
                      echo json_encode($arr);
                      exit;
                    }else{
                       $arr=array(
                        'error'=>2,
                        'info'=>'修改密码失败'
                        );
                      echo json_encode($arr);
                      exit; 
                    }
             }else{
                $arr=array(
                        'error'=>3,
                        'info'=>'无法修改'
                        );
                      echo json_encode($arr);
                      exit; 
                    }

    }
   // 会员停用
   public function member_stop(){
    $id=I('post.id');
    $res=M('user')->where(array('id'=>$id))->save(array('status'=>1));
            if ($res) {
                $arr=array(
                  'error'=>1,
                  'info'=>'已停用'
                    );
                echo json_encode($arr);
                exit;
            }else{
                $arr=array(
                  'error'=>2,
                  'info'=>'停用失败'
                    );
                echo json_encode($arr);
                exit;
            }

   }
   public function member_start(){
       $id=I('post.id');
       // var_dump($id);
    $res=M('user')->where(array('id'=>$id))->save(array('status'=>0));
            if ($res) {
                $arr=array(
                  'error'=>1,
                  'info'=>'已启用'
                    );
                echo json_encode($arr);
                exit;
            }else{
                $arr=array(
                  'error'=>2,
                  'info'=>'启用失败'
                    );
                echo json_encode($arr);
                exit;
            }

   }
   // 会员暂时删除
   public function  member_dele(){
      $id=I('post.id');
      if (M('user')->where(array('id'=>$id))->find()) {
         M('user')->where(array('id'=>$id))->save(array('del_status'=>'1')); 
             $arr=array(
                'error'=>1,
                'info'=>'删除成功'
                );
             echo json_encode($arr);
             exit;
      }else{
            $arr=array(
                'error'=>2,
                'info'=>'删除失败'
                );
             echo json_encode($arr);
             exit;
      }
   }
   // 会员恢复
   public  function member_recover(){
      $id=I('post.id');
      if (M('user')->where(array('id'=>$id))->find()) {
          M('user')->where(array('id'=>$id))->save(array('del_status'=>'0'));
          $arr=array(
            'error'=>1,
            'info'=>'恢复成功'
            );
          echo json_encode($arr);
          exit;
      }else{
          $arr=array(
            'error'=>2,
            'info'=>'恢复失败'
            );
          echo json_encode($arr);
          exit;
      }


   }


   // 管理员列表
   public function admin_list(){
    
    $count= M('user')->where('is_admin=1')->count();
    $Page       = new \Think\Page($count,3);
    $show       = $Page->show();// 分页显示输出
    $this->assign('pages',$show);

    // $list =M('user')->where('is_admin=1')->order('create_time')->limit($Page->firstRow.','.$Page->listRows)->select();

   $admins= M('user t1')->join('auth_group_access t2 on t1.id = t2.uid')->where(array('t1.is_admin'=>'1'))->field('t1.*,t2.name')->order('t1.id ')->limit($Page->firstRow.','.$Page->listRows)->select();

     // $sql='select t1.*,t2.name from user t1 inner join auth_group_access t2 on t1.id = t2.uid where t1.is_admin="1" ';
     // $admins=M()->query($sql);
     $this->assign('admins',$admins);
     $this->show();
   }
   // 添加管理员
   public function admin_newadd(){


    $this->show();
   }
   // 管理员批量删除核对
   public  function admin_checkdel(){
     $str= I('post.');
     $str=rtrim($str);
     if (M('user')->delete($str)) {
      $arr=array(
        'error'=>1,
        'info'=>'已删除！'
        );
      echo json_encode($arr);
      exit;
     }else{
      $arr=array(
        'error'=>2,
        'info'=>'删除失败'
        );
      echo json_encode($arr);
      exit;
     }



   }
   // 核对管理员信息
   public function admin_check(){
        // var_dump(I('post.'));
         $nickname=I('post.username');
         $email=I('post.email');
         $password=md5(I('post.pass'));
         $name=I('post.role');
        if (M('user')->where(array('nickname'=>$nickname))->find()) {
          $arr=array(
             'error'=>1,
             'info'=>'用户名已存在'
            );
          echo json_encode($arr);
          exit;
        }else if(M('user')->where(array('email'=>$email))->find()) {
            $arr=array(
             'error'=>2,
             'info'=>'邮箱已被使用'
            );
          echo json_encode($arr);
          exit;
        }else{
            $data['nickname']=$nickname;
            $data['email']=$email;
            $data['create_time']=time();
            $data['password']=$password;
            $data['is_admin']='1';
           $uid= M('user')->add($data);
            M('auth_group_access')->add(array('uid'=>$uid,'role'=>$role));
            $arr=array(
             'error'=>3,
             'info'=>'添加成功'
            );
          echo json_encode($arr);
          exit;
        }


   }
   // 管理员禁用
   public function admin_stop(){
    $id=I('post.id');

    if (M('user')->where(array('id'=>$id))->find()) {
       M('user')->where(array('id'=>$id))->save(array('is_admin'=>'0'));
       $arr=array(
            'error'=>1,
            'info'=>'停用成功'
        );
       echo json_encode($arr);
       exit;
    }else{
      $arr=array(
            'error'=>2,
            'info'=>'停用失败'
        );
       echo json_encode($arr);
       exit;
    }
    
   }
   // 管理员权限规则
   public function admin_rule(){

    $count      = M('auth_rule')->count();// 查询满足要求的总记录数
    $Page       = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(5)
    $show       = $Page->show();// 分页显示输出
    // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
    $rules = M('auth_rule')->order('id')->limit($Page->firstRow.','.$Page->listRows)->select();
    $this->assign('page',$show);// 赋值分页输出

     $this->assign('rules',$rules);
     $this->show();
   }
   // 规则增加
   public function rule_add(){
       $data=I('post.');
       if($id=M('auth_rule')->add($data)){
            $arr=array(
             'error'=>1,
             'info'=>'增加成功',
             'id'=>$id,
              );
            echo json_encode($arr);
            exit;
       }else{
            $arr=array(
             'error'=>2,
             'info'=>'增加失败'
              );
            echo json_encode($arr);
            exit;
       }
   }
   // 规则删除
   public  function  rule_del(){
     $id=I('post.id');
     if (M('auth_rule')->delete($id)) {
       $arr=array(
           'error'=>1,
           'info'=>'已删除！' 
        );
       echo json_encode($arr);
       exit;
     }else{
          $arr=array(
           'error'=>2,
           'info'=>'删除失败' 
        );
       echo json_encode($arr);
       exit;
     }
   }
   // 规则批量删除
   public function rule_delAll(){ 
    $str=I('post.str');
    $str=rtrim($str,",");
   if (M('auth_rule')->delete($str)) {
      $arr=array(
         'error'=>1,
         'info'=>'删除成功'
        );
      echo json_encode($arr);
      exit;
    }else{
       $arr=array(
         'error'=>2,
         'info'=>'删除失败'
        );
      echo json_encode($arr);
      exit;
    };
 


   }

   // 权限分类
    public function admin_cate(){
     $sql='select * from auth_group';
     $cates=M()->query($sql);
     $this->assign('cates',$cates);
     $this->show();
    }

   // 角色管理
   public function  admin_role(){
      $sql='select t1.*,t2.nickname from auth_group_access t1 inner join user t2 on t1.uid =t2.id';
      $roles=M()->query($sql);
      $this->assign('roles',$roles);

      $this->show();
   }

   // 添加角色
   public  function role_add(){
       

    $this->show();
   }
   // 角色编辑
   public  function role_edit(){

           $rule_list=M('auth_rule')->select();
           foreach ($rule_list as $v) {
             $cates[$v['category']][]=$v;
           }
           // var_dump($cates);
           $this->assign('cates',$cates);


        //查看所传用户已经拥有的权限
          $uid=I('get.uid');

          $res=M('auth_group_access')->where(array('uid'=>$uid))->find();
           
          $rule=M('auth_group')->where(array('id'=>$res['group_id']))->find();
          
          $rules=explode(',',$rule['rules']);
          $this->assign('rules',$rules);
          $this->assign('res',$res);
            $this->show();
   }
   // 角色编辑核对
   public function  role_checked(){
     $str=I('post.temp');//选择权限的id
     $str=rtrim($str,',');

     $data=I('post.data');
     $uid=$data['uid'];//管理员的id
     $res= M('auth_group_access')->where(array('uid'=>$uid))->find();//对权限关系表查询group是否为初始值
      if ($res['group_id']!=1) {
        $group_id=$res['uid'];
        $group['rules']=$str;
        $res=M('auth_group')->where(array('id'=>$res['group_id']))->save($group);
         if ($res){
           $arr=array(
                    'error'=>1,
                    'info'=>'修改成功'
                    );
                  echo  json_encode($arr);
                  exit;
         }else{
             $arr=array(
                    'error'=>2,
                    'info'=>'修改失败'
                    );
                  echo  json_encode($arr);
                  exit;
         }
     }else{
     $res=M('user')->where(array('id'=>$uid))->find();
     $group['title']=$res['email'];//找出用户的email
     $group['rules']=$str;    
     $id=M('auth_group')->add($group);
               if(M('auth_group_access')->where(array('uid'=>$uid))->save(array('group_id'=>$id))){
                 $arr=array(
                  'error'=>1,
                  'info'=>'修改成功'
                  );
                echo  json_encode($arr);
                exit;
             }else{
                $arr=array(
                  'error'=>2,
                  'info'=>'修改失败'
                  );
                echo  json_encode($arr);
                exit;
             };

     }  
     
    

   }
   // 管理员编辑
   public function  admin_edit(){
    $id=I('get.uid');
    $res=M('auth_group')->find($id);
    $this->assign('res',$res);
    $this->show();
   }
   

}